package com.aplink.generic.util.validation;

import android.widget.EditText;

import com.aplink.dellcommercial.R;
import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.ControlType;
import com.aplink.generic.constant.EditTextType;
import com.aplink.generic.database.DatabaseHelper;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.EditTextSpec;
import com.aplink.generic.widgets.BaseView;
import com.aplink.generic.widgets.CustomEdittext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Pattern;

public class ControlValidation {
    public static boolean isFormValidate( GenericFragment dataEntryActivity ) {
        boolean isValid = true;
        final ArrayList<ControlSpec> controlSpecList = dataEntryActivity
                .getControlSpecList();
        final HashMap<String, BaseView> viewsInLayout = dataEntryActivity
                .getViewsInLayout();

        for ( final ControlSpec controlSpec : controlSpecList ) {
            if ( controlSpec.getControlType() != ControlType.Button ) {

                final BaseView baseView = viewsInLayout.get( controlSpec
                        .getControlId() );
                final String contentData = baseView.getAttribute().getValue()
                        .toString();

                if ( baseView instanceof CustomEdittext ) {

                    final CustomEdittext customEdittext = (CustomEdittext) baseView;
                    EditTextSpec editTextSpecFormat = (EditTextSpec) controlSpec;
                    final EditText editText = customEdittext.getEditText();
                    final boolean isNoBlank = editTextSpecFormat.isNoBlank();
                    final boolean isNoDuplicate = editTextSpecFormat
                            .isNoDuplicate();
                    final int minLength = editTextSpecFormat.getMinLength();
                    final int maxLength = editTextSpecFormat.getMaxLength();
                    if ( isNoDuplicate ) {
                        final DatabaseHelper databaseHelper = dataEntryActivity
                                .getDBHelper();
                        final String tableName = dataEntryActivity.getTable();
                        final String keyAttribute = "";
                        boolean isExists = false;
                        // final boolean isExists =
                        // databaseHelper.isExists(genericObject,
                        // objPropCol).isObjectExists(
                        // tableName, keyAttribute, -1,
                        // controlSpec.getMappingName(), contentData);
                        if ( isExists ) {
                            editText.setError( "this value already exists in database, please try another value" );
                            isValid = false;
                            continue;
                        }
                    }
                    if ( isNoBlank && contentData.isEmpty() ) {
                        editText.setError( "This field is required" );
                        isValid = false;
                        continue;
                    }
                    if ( contentData.length() < minLength ) {
                        editText.setError( " longer than " + minLength
                                + " characters" );
                        isValid = false;
                        continue;
                    }
                    if ( contentData.length() > maxLength ) {
                        editText.setError( " no longer than " + maxLength
                                + " characters" );
                        isValid = false;
                        continue;
                    }

                    if ( editTextSpecFormat.getDataInputType().equals(
                            EditTextType.EMAIL ) ) {
                        final Pattern email = android.util.Patterns.EMAIL_ADDRESS;
                        if ( ! email.matcher( contentData ).matches() ) {
                            editText.setError( "This field is invalid, Please enter your email" );
                            isValid = false;
                            continue;
                        }
                    }

                    if ( editTextSpecFormat.getDataInputType().equals(
                            EditTextType.PHONE ) ) {
                        final Pattern email = android.util.Patterns.PHONE;
                        if ( ! email.matcher( contentData ).matches() ) {
                            editText.setError( "This field is invalid, Please enter your phone numbers" );
                            isValid = false;
                            continue;
                        }
                    }

                    if ( editTextSpecFormat.getDataInputType().equals(
                            EditTextType.PASSWORD ) ) {
                        String passwordRegex = dataEntryActivity
                                .getString( R.string.aplink__generic_password_regex );
                        final Pattern password = Pattern.compile( passwordRegex );
                        if ( ! password.matcher( contentData ).matches() ) {
                            String errorMessage = dataEntryActivity
                                    .getString( R.string.aplink__generic_invalid_pass );
                            editText.setError( errorMessage );
                            isValid = false;
                            continue;
                        }
                    }
                }
            }
        }
        return isValid;
    }

    public static boolean isFormValidate( GenericFragment dataEntryActivity,
                                          String validationGroupName ) {
        boolean isValid = true;
        final ArrayList<ControlSpec> controlSpecList = dataEntryActivity
                .getControlSpecList();
        final HashMap<String, BaseView> viewsInLayout = dataEntryActivity
                .getViewsInLayout();

        for ( final ControlSpec controlSpec : controlSpecList ) {
            if ( ( controlSpec.getControlType() != ControlType.Button )
                    && ( controlSpec.getValidationGroup()
                    .equalsIgnoreCase( validationGroupName ) ) ) {

                final BaseView baseView = viewsInLayout.get( controlSpec
                        .getControlId() );
                final String contentData = baseView.getAttribute().getValue()
                        .toString();

                if ( baseView instanceof CustomEdittext ) {

                    final CustomEdittext customEdittext = (CustomEdittext) baseView;
                    EditTextSpec editTextSpecFormat = (EditTextSpec) controlSpec;
                    final EditText editText = customEdittext.getEditText();
                    final boolean isNoBlank = editTextSpecFormat.isNoBlank();
                    final boolean isNoDuplicate = editTextSpecFormat
                            .isNoDuplicate();
                    final int minLength = editTextSpecFormat.getMinLength();
                    final int maxLength = editTextSpecFormat.getMaxLength();
                    if ( isNoDuplicate ) {
                        final DatabaseHelper databaseHelper = dataEntryActivity
                                .getDBHelper();
                        final String tableName = dataEntryActivity.getTable();
                        final String keyAttribute = "";
                        final boolean isExists = false;
                        // databaseHelper.isObjectExists(
                        // tableName, keyAttribute, -1,
                        // controlSpec.getMappingName(), contentData);
                        if ( isExists ) {
                            editText.setError( "this value already exists in database, please try another value" );
                            isValid = false;
                            continue;
                        }
                    }
                    if ( isNoBlank && contentData.isEmpty() ) {
                        editText.setError( "This field is required" );
                        isValid = false;
                        continue;
                    }
                    if ( contentData.length() < minLength ) {
                        editText.setError( " longer than " + minLength
                                + " characters" );
                        isValid = false;
                        continue;
                    }
                    if ( contentData.length() > maxLength ) {
                        editText.setError( " no longer than " + maxLength
                                + " characters" );
                        isValid = false;
                        continue;
                    }

                    if ( editTextSpecFormat.getDataInputType().equals(
                            EditTextType.EMAIL ) ) {
                        final Pattern email = android.util.Patterns.EMAIL_ADDRESS;
                        if ( ! email.matcher( contentData ).matches() ) {
                            editText.setError( "This field is invalid, Please enter your email" );
                            isValid = false;
                            continue;
                        }
                    }

                    if ( editTextSpecFormat.getDataInputType().equals(
                            EditTextType.PHONE ) ) {
                        final Pattern email = android.util.Patterns.PHONE;
                        if ( ! email.matcher( contentData ).matches() ) {
                            editText.setError( "This field is invalid, Please enter your phone numbers" );
                            isValid = false;
                            continue;
                        }
                    }

                    if ( editTextSpecFormat.getDataInputType().equals(
                            EditTextType.PASSWORD ) ) {
                        String passwordRegex = dataEntryActivity
                                .getString( R.string.aplink__generic_password_regex );
                        final Pattern password = Pattern.compile( passwordRegex );
                        if ( ! password.matcher( contentData ).matches() ) {
                            String errorMessage = dataEntryActivity
                                    .getString( R.string.aplink__generic_invalid_pass );
                            editText.setError( errorMessage );
                            isValid = false;
                            continue;
                        }
                    }
                }
            }
        }
        return isValid;
    }
}
